package com.kelvem.saas.workbanch.ontology.order.event;

import com.kelvem.saas.workbanch.fsm.anno.Fsm;
import com.kelvem.saas.workbanch.fsm.anno.state.EventState;
import com.kelvem.saas.workbanch.fsm.anno.event.RegisterEvent;
import com.kelvem.saas.workbanch.fsm.anno.state.EventStateGroup;
import com.kelvem.saas.workbanch.ontology.order.handle.address.CheckAddressHandle;
import com.kelvem.saas.workbanch.ontology.order.handle.core.ConvRequest2OrderHandle;
import com.kelvem.saas.workbanch.ontology.order.handle.core.PersistenceOrderHandle;
import com.kelvem.saas.workbanch.ontology.order.handle.core.TestOrderHandle;
import com.kelvem.saas.workbanch.ontology.order.handle.create.CreateOrderNoHandle;
import com.kelvem.saas.workbanch.ontology.order.handle.ship.EstimateShipPriceHandle;
import com.kelvem.saas.workbanch.ontology.order.model.OrdersEntity;
import com.kelvem.saas.workbanch.ontology.order.request.OrderPaymentReq;
import com.kelvem.saas.workbanch.ontology.order.request.OrdersReq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @author kelvem
 */
@Slf4j
@Fsm(entityClazz=OrdersEntity.class, projectName="订单系统")
// 正常状态
@EventState(stateCode ="start", stateName ="起始", type="normal", start=1, timeout=600)
@EventState(stateCode ="pending", stateName ="待收款")
@EventState(stateCode ="payment", stateName ="已支付")
@EventState(stateCode ="confirm", stateName ="确认收款")
@EventState(stateCode ="matched", stateName ="已配货")
@EventState(stateCode ="send_to_wh", stateName ="发送库房")
@EventState(stateCode ="shipped", stateName ="已发货")
@EventState(stateCode ="received", stateName ="已收货")
// 异常状态
@EventState(stateCode ="suspend_payment", stateName ="收款暂停", type="abnormal", timeout=24*60*60)
@EventState(stateCode ="suspend_wh", stateName ="发货暂停", type="abnormal", timeout=2*24*60*60)
@EventState(stateCode ="cancel", stateName ="已取消", type="abnormal", end=1)
@EventState(stateCode ="returned", stateName ="已退货", type="abnormal", end=1)
// 正常状态组
@EventStateGroup(groupCode ="all", groupName ="全部状态", states={"before_shipped", "after_shipped", "abnormal"})
@EventStateGroup(groupCode ="before_shipped", groupName ="发货前状态", states={"stage_pending","stage_payment","stage_wh"})
@EventStateGroup(groupCode ="stage_pending", groupName ="下单阶段", states={"start","pending"})
@EventStateGroup(groupCode ="stage_payment", groupName ="收款阶段", states={"payment", "confirm", "suspend_payment"})
@EventStateGroup(groupCode ="stage_wh", groupName ="发货前阶段", states={"matched","send_to_wh", "suspend_wh"})
@EventStateGroup(groupCode ="after_shipped", groupName ="发货后状态", states={"shipped", "received", "returned"})
@EventStateGroup(groupCode ="abnormal", groupName ="异常状态", states={"suspend_payment", "suspend_wh", "cancel", "returned"})
// 异常状态组
@EventStateGroup(groupCode ="cancel_able", groupName ="可取消", states={"stage_payment", "stage_wh"})
@EventStateGroup(groupCode ="suspend_able", groupName ="可暂停", states={"stage_payment", "stage_wh"})
public class OrdersEvent {
    
    @RegisterEvent(eventCode ="Orders.create", eventName="创建订单", srcState="start", dstState="pending",
            beforeHandles = {ConvRequest2OrderHandle.class},
            handles = {CreateOrderNoHandle.class, CheckAddressHandle.class, EstimateShipPriceHandle.class},
            afterHandles = {PersistenceOrderHandle.class, TestOrderHandle.class}
    )
    public void create(OrdersReq ordersReq) {
        // do nothing
        log.info("create method do nothing");
    }
    
    @RegisterEvent(eventCode = "Orders.payment", eventName="支付订单", srcState="pending", dstState="payment")
    public void payment(OrderPaymentReq payment) {
        // do nothing
        log.info("payment method do nothing");
    }
    
    @RegisterEvent(eventCode = "Orders.cancel", eventName="取消订单", srcState="cancel_able", dstState="cancel")
    public void cancel(Long ordersId) {
        // do nothing
        log.info("cancel method do nothing");
    }
    
    @RegisterEvent(eventCode = "Orders.suspend", eventName="暂停订单", srcState="stage_payment", dstState="suspend_payment")
    public void suspend(Object suspendReq) {
        // do nothing
        log.info("suspend method do nothing");
    }
}
